{include file="public/head" /}
<style>#imgDiv{margin-top: 20px;height: 390px;text-align: center;line-height: 390px;padding: 0}
       #img{height: 389px;max-width: 100%}
</style>

<title>新闻管理</title>
</head>
<body>
<div id="right_ctn">
    <div class="right_m">
        <div class="hy_list">
            <div class="box_t">
                <span class="name">新闻管理</span>
                <!--当前位置-->
                <div class="position">
                    <a><img src="__STATIC__/admin/images/icon5.png" alt=""/></a>
                    <a href="{:url('admin/Index/index')}" target="_top">首页</a>
                    <span><img src="__STATIC__/admin/images/icon3.png" alt=""/></span>
                    <a href="{:url('admin/NewsController/index')}">新闻管理</a>
                    <span><img src="__STATIC__/admin/images/icon3.png" alt=""/></span>
                    <a href="{:url('admin/NewsController/add')}">发布新闻</a>
                </div>
                <!--当前位置-->
            </div>
            <div class="space_hx">&nbsp;</div>
            <div class="xjhy" style="padding:0px;">
                <div class="tabBox_t">
                    <div class="tabBox">
                        <ul class="tabNav">
                            <li class="now"><span><a href="{:url('admin/NewsController/add')}">添加新闻</a></span></li>
                        </ul>
                        <div class="tabCont" style="display:block;height: 650px;">
                           <form action="{:url('admin/NewsController/addPost')}" enctype="multipart/form-data" method="post" id="form">
                               <div class="col-md-5">
                                   <div class="form-group">
                                       <label for="type_id"><span class="form-required">*</span>类别</label>
                                       <select name="type_id" id="type_id" class="form-control">
                                           <option value="" hidden>请选择类别</option>
                                           {volist name="typeList" id="vo"}
                                           <option value="{$vo.id}">{$vo.name}</option>
                                           {/volist}
                                       </select>
                                   </div>
                                   <div class="form-group">
                                       <label for="dep_id"><span class="form-required">*</span>部门</label>
                                       <select name="dep_id" id="dep_id" class="form-control">
                                           <option value="" hidden>请选择部门</option>
                                           {volist name="depList" id="vo"}
                                           <option value="{$vo.id}">{$vo.name}</option>
                                           {/volist}
                                       </select>
                                   </div>
                                   <div class="form-group">
                                       <label for="title"><span class="form-required">*</span>新闻标题</label>
                                       <input type="text" id="title" name="title" class="form-control">
                                   </div>
                                   <div class="form-group">
                                       <label for="content"><span class="form-required">*</span>新闻内容</label>
                                       <!-- 加载UEditor编辑器的容器 -->
                                       <script id="content" name="content" type="text/plain"></script>
                                   </div>
                               </div>
                               <div class="col-md-7" style="margin-top: 60px">
                                   <div class="form-group">
                                       <label for="image">&nbsp;添加图片</label>
                                       <input type="file" id="image" name="image" accept="image/png, image/jpeg"/>
                                       <div class="form-control" id="imgDiv"><img src="" id="img" hidden></div>
                                   </div>
                                   <div class="form-group">
                                       <a type="submit" class="btn btn-primary" onclick="add()">添加</a>
                                       <a class="btn btn-default" href="{:url('admin/NewsController/index')}">返回</a>
                                   </div>
                               </div>
                           </form>
                            <script>
                                function add() {
                                    var form = $("#form");
                                    //获取表单数据
                                    var formData = form.serializeArray();
                                    for(var i=0;i<formData.length;i++){
                                        var value = formData[i].value;
                                        //若某个input未输入,则弹窗提示
                                        if(value === ''){
                                            //循环时,获取当前所对应的input标签的上一个同级标签的text值,即所对应的label的文本值
                                            var msg = $("#"+formData[i].name).prev().text();
                                            layer.msg('请输入'+msg,{time:2000});
                                            return false;
                                        }
                                    }
                                    var load = layer.load(0, {shade: false});
                                    var url = form.attr('action');
                                    var type = form.attr('method');

                                    //获取图片信息，若存在图片则先执行一次ajax发送图片，再ajax发送新闻内容
                                    //若不存在图片，则直接执行ajax发送新闻内容
                                    var file = $('#image')[0].files;
                                    if(file.length>0) {
                                        var image = new FormData();
                                        image.append('image', file[0]);
                                        $.ajax({
                                            type: type,
                                            url: url,
                                            data: image,
                                            dataType: "json",
                                            processData: false,//用于对data参数进行序列化处理 这里必须false
                                            contentType: false, //必须
                                            success: function (result) {
                                                var imgName = result.data;
                                                formData.push({name:'imgName',value:imgName});
                                                $.ajax({
                                                    type:type,
                                                    url:url,
                                                    data:formData,
                                                    dataType:"json",
                                                    success:function (result) {
                                                        //如果返回值中有url,则跳转到该url
                                                        if(result.url !== ''){
                                                            layer.close(load);
                                                            layer.msg(result.msg,{time:1500});
                                                            setTimeout(function () {
                                                                window.location.href = result.url;
                                                            },1600);
                                                        }
                                                    }
                                                });
                                            }
                                        });
                                    }else{
                                        formData.push({name:'imgName',value:''});
                                        $.ajax({
                                            type:type,
                                            url:url,
                                            data:formData,
                                            dataType:"json",
                                            success:function (result) {
                                                //如果返回值中有url,则跳转到该url
                                                if(result.url !== ''){
                                                    layer.close(load);
                                                    layer.msg(result.msg,{time:1500});
                                                    setTimeout(function () {
                                                        window.location.href = result.url;
                                                    },1600);
                                                }
                                            }
                                        });
                                    }
                                }
                            </script>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $('#image').change(function () {
        //获取当前上传图片
        var imgData = this.files[0];
        //新建一个文件读取对象
        var imgRead = new FileReader();
        //要读取的文件对象
        imgRead.readAsDataURL(imgData);
        //当读取成功时，创建image对象，将读取到的图片信息放入src
        imgRead.onload = function() {
            var aImage = new Image();
            aImage.src = this.result;

            $('#img').removeAttr('hidden');
            $('#img').attr('src',aImage.src);
        };
    });
</script>
<!-- UEditor配置文件 -->
<script type="text/javascript" src="__STATIC__/admin/ueditor-php/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="__STATIC__/admin/ueditor-php/ueditor.all.js"></script>
<!-- 实例化编辑器 -->
<script type="text/javascript">
    var ue = UE.getEditor('content',{
        initialFrameHeight:280,
        initialFrameWidth: null,
        autoHeightEnabled:false,
        maximumWords:800
    });
</script>
</body>
</html>
